<?php
	/*
	// File:	dump-0.5.php
	// Purpose:	Importing a 0.5 dump into 0.6
	// Creation:	2007-03-30
	// Author:	Gyrbo <dennis@gyrbo.be>
	*/

/*
 * Run this script from the commandline from it original location
 */

 /* TODO:
    * Import mailman table
    * set update flag and signal backend so that require config files are created
  */

chdir(dirname(__FILE__));

// Basic initialization
require_once('../web/functions.inc.phps');
require_once('../web/config.inc.php');

mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass']);
mysql_select_db($cfg['dbname']);

$fp = fopen('../webcp-upgrade.dump', 'rb');
if(!$fp)
	die('Make sure "webcp-upgrade.dump" is in the web-cp root folder!'."\n");

fgets($fp); //ignore first line

function unserialize_if_possible(&$item) {
	$res = unserialize($item);
	if($res!==false) {
		$item = $res;
	}
}

while(!feof($fp)) {
	$line = trim(fgets($fp));
	$section = substr($line, 1, -1);
	switch($section) {
	case 'resellers':
		$header = fgetcsv($fp);
		while(($line = fgetcsv($fp))!=array(null)) {
			$data = array_combine($header, $line);
			//array_walk($data, 'unserialize_if_possible');

			$query_fields = array('id', 'name', 'email', 'ip', 'domains', 'subdomains', 'pointers',
				'users', 'hd', 'serverside', 'db', 'shell', 'skin', 'hold', 'lastchange');

			$sql = 'INSERT INTO resellers (';
			foreach($query_fields as $field) {
				if(isset($data[$field]))
					$sql .= '`'.$field.'`, ';
			}
			$sql = substr($sql, 0, -2).') VALUES (';
			foreach($query_fields as $field) {
				if(!isset($data[$field]))
					continue;
				$value = $data[$field];
				if(strval(intval($value))!=$value) { //is_numeric is not strict enough
					$value = "'".mysql_escape_string($value)."'";
				}
				$sql .= $value.', ';
			}

			$sql = substr($sql, 0, -2).')';
			mysql_query($sql) or print(mysql_error())."\n";

			if(isset($data['bandwidth']))
				set_property('bandwith', $data['bandwith'], $data['id'], 'bandwith');
			if(isset($data['transfer']))
				set_property('transfer', $data['transfer'], $data['id'], 'transfer');
		}
		break;

	case 'domains':
		$header = fgetcsv($fp);
		while(($line = fgetcsv($fp))!=array(null)) {
			$data = array_combine($header, $line);
			//array_walk($data, 'unserialize_if_possible');

			$query_fields = array('id', 'owner', 'priority', 'ip', 'host', 'host1',
				'domain', 'domain1', 'aliases', 'catchall', 'nohost', 'redirect',
				'path', 'defaultroot', 'type', 'hd', 'users', 'subdomains', 'pointers',
				'hd_used', 'serverside', 'db', 'shell', 'action', 'hash', 'hold',
				'httpcustom', 'email', 'suspended', 'time');

			$sql = 'INSERT INTO domains (';
			foreach($query_fields as $field) {
				if(isset($data[$field]))
					$sql .= '`'.$field.'`, ';
			}
			$sql = substr($sql, 0, -2).') VALUES (';
			foreach($query_fields as $field) {
				if(!isset($data[$field]))
					continue;
				$value = $data[$field];
				if(strval(intval($value))!=$value) { //is_numeric is not strict enough
					$value = "'".mysql_escape_string($value)."'";
				}
				$sql .= $value.', ';
			}

			$sql = substr($sql, 0, -2).')';
			mysql_query($sql) or print(mysql_error())."\n";


			if(isset($data['bandwidth']))
				set_property('bandwith', $data['bandwith'], $data['id'], 'bandwith');
			if(isset($data['transfer']))
				set_property('transfer', $data['transfer'], $data['id'], 'transfer');
		}
		break;

	case 'users':
		$header = fgetcsv($fp);
		while(($line = fgetcsv($fp))!=array(null)) {
			$data = array_combine($header, $line);
			//array_walk($data, 'unserialize_if_possible');

			$query_fields = array('username', 'username1', 'password', 'id', 'name',
				'level', 'type', 'shell', 'root', 'hd', 'hd_used', 'aliases', 'catchall',
				'autoreply', 'forward', 'favorites', 'lang', 'skin', 'webcp_tag',
				'timeout', 'remote_addr', 'action', 'hash', 'suspend', 'time', 'ucount',
				'ip_restrict');

			$sql = 'INSERT INTO users (';
			foreach($query_fields as $field) {
				if(isset($data[$field]))
					$sql .= '`'.$field.'`, ';
			}
			$sql = substr($sql, 0, -2).') VALUES (';
			foreach($query_fields as $field) {
				if(!isset($data[$field]))
					continue;
				$value = $data[$field];
				if(strval(intval($value))!=$value) { //is_numeric is not strict enough
					$value = "'".mysql_escape_string($value)."'";
				}
				$sql .= $value.', ';
			}

			$sql = substr($sql, 0, -2).')';
			mysql_query($sql) or print(mysql_error())."\n";

			set_property('unix user', $data['username'], $data['id'], $data['username']);
		}
		break;
	}
}

?>